package ouc.pkere.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ouc.pkere.entity.RelationEntity;
import ouc.pkere.mapper.RelationMapper;
import ouc.pkere.mysqlmanager.controller.Connector;

import java.util.*;

@Service
public class VisualizationService {


    @Autowired
    RelationMapper relationMapper;


    public Map<String, Object> getDetail(String value) throws Exception {

        Map<String, String> relation = relationMapper.findOneByValue1(value);
        Map<String, Object> allColumnValue = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        Connector connector = new Connector();

        /*
         * MARK 部署前修改
         */
        connector.connect("127.0.0.1", "3306", "root", "password", "pkdb");
        Map<String, String> columnComment = connector.getColumnCommentByTableName(relation.get("table_name_1"));
        connector.disconnect();
        allColumnValue = relationMapper.getValueDetail("`" + relation.get("table_name_1") + "`", "`" + relation.get("primary_key_col_1") + "`",
                relation.get("row_index_1"));
        if (columnComment == null || columnComment.isEmpty()) {
            return allColumnValue;
        } else {
            for (String key : columnComment.keySet()) {
                if (columnComment.get(key) != null && !columnComment.get(key).equals("")) {
                    result.put(columnComment.get(key), allColumnValue.get(key));
                }
            }
        }

        return result;

    }

    public Map<String, Object> getAllDataFromDatabase() {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, String>> dataMap = relationMapper.findAllValue();
        List<Map<String, String>> linkMap = relationMapper.findAllAsVisualizeFormat();

        map.put("nameData", dataMap);
        map.put("linkData", linkMap);
        return map;
    }

    public Map<String, Object> getDataByValueFromDatabase(String value, int depth) {
        Map<String, Object> map = new HashMap<>();

        List<Map<String, String>> dataMap = new ArrayList<>();
        List<Map<String, String>> linkMap = new ArrayList<>();
        List<String> valueList = new ArrayList<>();
        List<String> valueListNext = new ArrayList<>();

        valueListNext.add(value);

        List<Map<String, String>> tableDataList = relationMapper.findByValue1(value);

        //将每一层级的数据放入map中
        for (int i = 0; i <= depth; i++) {

            Set<String> tempValueSet = new HashSet<>();

            //查询每一层所有姓名的关系
            for (int j = 0; j < valueListNext.size(); j++) {
                tableDataList.clear();
                tableDataList = relationMapper.findByValue1(valueListNext.get(j));
                for (int k = 0; k < tableDataList.size(); k++) {
                    String targetValue = tableDataList.get(k).get("target");
                    //如果找到的数据的value_2(targetValue)不是前几层出现过的名字，加入临时列表，等待循环结束后全部放入valuelist和valuelistnext
                    if (!valueList.contains(targetValue)) {
                        tempValueSet.add(targetValue);
                    }
                    linkMap.add(tableDataList.get(k));
                }
            }

            //如果是最后一层，下一层value不会添加到valuelist中
            if (i < depth) {
                valueList.addAll(tempValueSet);
                valueListNext.clear();
                valueListNext.addAll(tempValueSet);
            }

        }


        for (int i = 0; i < valueList.size(); i++) {
            Map<String, String> mapToPutInDataList = new HashMap<String, String>();
            mapToPutInDataList.put("name", valueList.get(i));
            if (valueList.get(i).equals(value)) {
                mapToPutInDataList.put("category", "center");
            }

            dataMap.add(mapToPutInDataList);
        }

        map.put("nameData", dataMap);
        map.put("linkData", linkMap);
        return map;
    }
}
